package online.exam.controller;

import cn.hutool.http.HttpStatus;
import online.exam.common.GlobalConstant;
import online.exam.pojo.Student;
import online.exam.pojo.bo.StudentBO;
import online.exam.service.StudentService;
import online.exam.utils.PageUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

/**
 * @author 王景煜
 * created in 2019/11/14 19:29
 */

@RequestMapping("/student")
@RestController
public class StudentController {

    @Autowired
    private StudentService studentServiceImpl;

    @GetMapping("/queryByNoAndPwd")
    public int queryByNoAndPwd(StudentBO studentBO, HttpServletRequest request) {
        HttpSession session = request.getSession();
        Student student = studentServiceImpl.queryByNoAndPwd(studentBO);
        if (student != null) {
            session.setAttribute(GlobalConstant.STUDENT, student);
            return HttpStatus.HTTP_OK;
        }
        return HttpStatus.HTTP_INTERNAL_ERROR;
    }

    @GetMapping("/queryStuByNoAndPwd")
    public Student queryStuByNoAndPwd(StudentBO studentBO) {
        return studentServiceImpl.queryByNoAndPwd(studentBO);
    }

    @GetMapping("/isLogin")
    public Student isLogin(HttpServletRequest request) {
        return (Student) request.getSession().getAttribute(GlobalConstant.STUDENT);
    }

    @GetMapping("/queryAll/{page}/{limit}")
    public PageUtils<Student> queryAll(@PathVariable Integer page, @PathVariable Integer limit) {
        return studentServiceImpl.queryAll(page, limit);
    }

    @GetMapping("/queryByNo")
    public Student queryByNo(@RequestParam String studentNo) {
        return studentServiceImpl.queryByNo(studentNo);
    }

    @PostMapping("/addStudent")
    public int addStudent(Student student) {
        return studentServiceImpl.addStudent(student);
    }

    @PostMapping("/updateStudent")
    public int updateStudent(Student student) {
        return studentServiceImpl.updateStudent(student);
    }

    @PostMapping("/deleteStudent")
    public int deleteStudent(@RequestParam String id) {
        return studentServiceImpl.deleteStudent(id);
    }

}
